/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package juggernaut;

import java.io.*;
import java.math.*;
import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.planx.xmlstore.routing.*;
import tokyocabinet.*;

/**
 *
 * @author Administrator
 */
public class Main {

    //
    // create the object
    //
    /**
     * @param args the command line arguments
     */
    public static int counter = 0;
    public static String data = "KeyGenerated";

    public static void main(String[] args) throws NoSuchAlgorithmException {
        if(args.length<4){
            System.out.println("Usage:PlanY.main <SocketPort> <DHTBasePort> <numberofDHTs> [dbLocation] [<bootStrapNodeIP><bootStrapNodePort>] ");
        }
        Thread[] threads = new Thread[10];
        Kademlia[] kads = new Kademlia[10];
        MessageDigest md5;
        int serverPort = 13001;
        int basePort = Integer.parseInt(args[1]);
        int numNodes = Integer.parseInt(args[2]);
        String bootStrapNode = null;
        String location ="";
        int bootStrapNodePort = -1;
        if(args.length >4){
            bootStrapNode=args[4];
            bootStrapNodePort= Integer.parseInt(args[5]);
        }
        if(args.length>3){
            location=args[3];
            System.out.println("location of DB files is"+location);
        }
        try {
            for (int i = 0; i < numNodes; i++) {
                Kademlia kad1 = new Kademlia("Node" +basePort + i, Identifier.randomIdentifier(), basePort + i, null, null,location);
                if (bootStrapNode != null) {
                    kad1.connect(new InetSocketAddress( bootStrapNode, bootStrapNodePort));
                }else{
                    if(i>0){
                         kad1.connect(new InetSocketAddress( "127.0.0.1", basePort));
                    }
                }
                System.out.println("Created Node" + i);
                kads[i] = kad1;
            }
            DHTServer.initializeServer(args[0],kads);
            for (int i = 0; i < 10; i++) {
                kads[i].close();
            }
        } catch (RoutingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

class FastPut implements Runnable {

    Kademlia kad;
    int startCount;
    int count;

    public FastPut(Kademlia kad, int startCount, int count) {
        this.kad = kad;
        this.startCount = startCount;
        this.count = count;
    }

    public void run() {
        int endCount = startCount + count + 1;
        for (int i = startCount; i < endCount; i++) {
            try {
                String key = "keyGenerated1" + i;
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(key.getBytes());
                byte[] md5Bytes = md5.digest();
                md5.reset();
                BigInteger bi = new BigInteger(1, md5Bytes);
                Identifier id = new Identifier(bi);
                kad.put(id, new MyObject());
            } catch (RoutingException ex) {
                Logger.getLogger(FastPut.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FastPut.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(FastPut.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}

class MyObject implements Serializable {

    String myData = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
}
